import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class Sort {
    //直接插入排序
    public static void insertSort(int[] array){
        int i =1;
        while(i<array.length){
            int j = i-1;
            int tmp = array[i];
            for(;j>=0;j--){
                if(array[j]>tmp){
                    array[j+1] = array[j];
                }else {
                    array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
            i++;
        }
    }

    //希尔排序
    public static void shellSort(int[] array){
        int gap = array.length;
        while(gap>1){
            gap /= 2;
            shell(array,gap);
        }
    }

    private static void shell(int[] array,int gap){
        int i = gap;
        while(i<array.length) {
            int j = i - gap;
            int tmp = array[i];
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    array[j + gap] = tmp;
                    break;
                }
            }
            array[j + gap] = tmp;
            i++;
        }
    }


    //选择排序
    public static void selectSort2(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            int minindex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j]<array[minindex]){
                    minindex = j;
                }
            }
            swap(array,i,minindex);
        }
    }

    //选择排序（优化版）
    public static void selectSort(int[] array){
        int right = array.length-1;
        int i = 0;
        while(i<right) {
            int minindex = i;
            int maxindex = i;
            for (int j = i; j < right+1; j++) {
                if(array[j]<array[minindex]){
                    minindex = j;
                }
                if(array[j]>array[maxindex]){
                    maxindex = j;
                }
            }
            swap(array,i,minindex);
            if(maxindex == i){
                maxindex = minindex;
            }
            swap(array,right,maxindex);
            right--;
            i++;
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //堆排序
    public static void heapSort(int[] array){
        createHeap(array);
        int end = array.length-1;
        while(end>0){
            swap(array,0,end);
            siftDown(array,0,end);
            end--;
        }
    }

    private static void createHeap(int[] array){
        for(int parent = (array.length-1-1)/2;parent>=0;parent--){
            siftDown(array,parent,array.length);
        }
    }

    private static void siftDown(int[] array,int parent,int size){
        int child = parent*2+1;
        while (child<size){
            if(child+1<size && array[child]<array[child+1]){
                //swap(array,child,child+1);
                child++;
            }
            if(array[child]>array[parent]){
                swap(array,child,parent);
                parent = child;
                child = parent*2+1;
            }else {
                break;
            }
        }
    }


    //冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int flag = 0;
            for (int j = 0; j < array.length-i-1; j++) {
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flag = 1;
                }
            }
            if(flag==0){
                break;
            }
        }
    }

    //快速排序
    public static void quickSort(int[] array){

        quickNor(array,0,array.length-1);
    }

    //非递归
    public static void quickNor(int[] array,int left,int right){
        Deque<Integer> stack = new ArrayDeque<>();

        int pivot = quickOperate(array,left,right);
        if(pivot > left+1){
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot < right-1){
            stack.push(pivot+1);
            stack.push(right);
        }

        while(!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            pivot = quickOperate(array,left,right);
            if(pivot > left+1){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot < right-1){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    private static void quick(int[] array,int left,int right){
        if(left>=right){
            return ;
        }
        int middle = getMiddle(array,left,right);
        //中位数与start交换，可以减少因为有序数组导致的多层递归
        swap(array,middle,left);
        int base = quickOperate(array,left,right);
        quick(array,left,base-1);
        quick(array,base+1,right);
    }

    private static int quickOperate(int[] array,int left,int right){
        int tmp = left;
        while(left<right){
            while (left<right && array[right]>=array[tmp]){
                right--;
            }
            while(left<right && array[left]<=array[tmp]){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,tmp);
        return left;
    }


    //获取每一次数组中的中位数
    private static int getMiddle(int[] array,int start,int end) {
        int middle = (start + end) / 2;
        if (array[start] < array[end]) {
            if (array[middle] > array[end]) {
                return end;
            } else if (array[middle] < array[start]) {
                return start;
            } else {
                return middle;
            }
        } else if (array[start] > array[end]) {
            if (array[middle] > array[start]) {
                return start;
            } else if (array[middle] < array[end]) {
                return end;
            } else {
                return middle;
            }
        }else{
            return end;
        }
    }

    //归并排序
    public static void mergeSort(int[] array){
        merge(array,0,array.length-1);
    }

    private static void merge(int[] array, int left, int right) {
        if (left >= right) {
            return ;
        }
        int middle = (left+right)/2;

        merge(array,left,middle);
        merge(array,middle+1,right);

        mergeChild(array,left,right,middle);
    }

    private static void mergeChild(int[] array,int left,int right,int middle){
        int s1 = left;
        int e1 = middle;
        int s2 = middle+1;
        int e2 = right;
        int[] tmp = new int[right-left+1];
        int k=0;
        while (e1 >= s1 && e2 >= s2) {
            if (array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }

        while(e1 >= s1){
            tmp[k++] = array[s1++];
        }
        while (e2 >= s2) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }

    //非递归 归并排序
    public static void mergeNorSort(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i + gap * 2) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid + gap;
                if(right >= array.length) {
                    right = array.length-1;
                }
                mergeChild(array,left,right,mid);
            }
            gap *= 2;
        }
    }
}
